<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;
use Gregwar\Captcha\CaptchaBuilder;

class LoginController extends Controller
{
    //显示后台登录界面
    public function getIndex()
    {
    	//解析模板
    	return view('admin.login.index');
    }

    /*
    	后台登录
	*/

    public function postLogin(Request $request)
    {

    	 // dd($request->all());
    	//根据用户名查询当前用户是否存在
    	 $users = DB::table('user')->where('username',$request->input('username'))->first();
    	   
    	// $users = DB::table('user')->get();
         // dd($users);
    	if(empty($users)){
    		return back()->with('error','用户名和不正确');
    	}
        // dd($request->input('password'));
    	// //判断用户名密码和输入密码是否一样
    	if(Hash::check($request->input('password'),$users->password)){
    		//登录成功
    		session(['id'=>$users->id]);
    	 	return redirect('/admin/user/index')->with('success','恭喜'.$users->username.'登录成功!!!');

    	}else{
    		return back()->with('error','用户名和密码不正确');
    	}
    	// dd($users);
    	

    }

    /*
    	后台登录验证码
    */

    public function vcode()
    {
    	// echo '222';
    	ob_clean();//清除输出缓存区的内容
	    //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        session(['Vcode'=>$phrase]);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();

       //看到图片输出 需要die
       // die;

       //判断验证码是否正确
        
       

    }


    /*
    	后台退出
    */
    public function out()
    {
    	// echo '222';
    	 session()->forget('id');
    	 return redirect('/admin/');
    }


}
